Merge "Prevent use of expiries to circumvent restrictions on removing user groups"
[lhc/web/wiklou.git] / tests / phpunit / includes / db / LBFactoryTest.php
1 <?php
2
3 use Wikimedia\Rdbms\ChronologyProtector;
4
5 /**
6 * Holds tests for LBFactory abstract MediaWiki class.
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 * http://www.gnu.org/copyleft/gpl.html
22 *
23 * @group Database
24 * @file
25 * @author Antoine Musso
26 * @copyright © 2013 Antoine Musso
27 * @copyright © 2013 Wikimedia Foundation Inc.
28 */
29 class LBFactoryTest extends MediaWikiTestCase {
30
31 /**
32 * @dataProvider getLBFactoryClassProvider
33 */
34 public function testGetLBFactoryClass( $expected, $deprecated ) {
35 $mockDB = $this->getMockBuilder( 'DatabaseMysqli' )
36 ->disableOriginalConstructor()
37 ->getMock();
38
39 $config = [
40 'class' => $deprecated,
41 'connection' => $mockDB,
42 # Various other parameters required:
43 'sectionsByDB' => [],
44 'sectionLoads' => [],
45 'serverTemplate' => [],
46 ];
47
48 $this->hideDeprecated( '$wgLBFactoryConf must be updated. See RELEASE-NOTES for details' );
49 $result = MWLBFactory::getLBFactoryClass( $config );
50
51 $this->assertEquals( $expected, $result );
52 }
53
54 public function getLBFactoryClassProvider() {
55 return [
56 # Format: new class, old class
57 [ 'LBFactorySimple', 'LBFactory_Simple' ],
58 [ 'LBFactorySingle', 'LBFactory_Single' ],
59 [ 'LBFactoryMulti', 'LBFactory_Multi' ],
60 ];
61 }
62
63 public function testLBFactorySimpleServer() {
64 global $wgDBserver, $wgDBname, $wgDBuser, $wgDBpassword, $wgDBtype, $wgSQLiteDataDir;
65
66 $servers = [
67 [
68 'host' => $wgDBserver,
69 'dbname' => $wgDBname,
70 'user' => $wgDBuser,
71 'password' => $wgDBpassword,
72 'type' => $wgDBtype,
73 'dbDirectory' => $wgSQLiteDataDir,
74 'load' => 0,
75 'flags' => DBO_TRX // REPEATABLE-READ for consistency
76 ],
77 ];
78
79 $factory = new LBFactorySimple( [ 'servers' => $servers ] );
80 $lb = $factory->getMainLB();
81
82 $dbw = $lb->getConnection( DB_MASTER );
83 $this->assertTrue( $dbw->getLBInfo( 'master' ), 'master shows as master' );
84
85 $dbr = $lb->getConnection( DB_SLAVE );
86 $this->assertTrue( $dbr->getLBInfo( 'master' ), 'DB_SLAVE also gets the master' );
87
88 $factory->shutdown();
89 $lb->closeAll();
90 }
91
92 public function testLBFactorySimpleServers() {
93 global $wgDBserver, $wgDBname, $wgDBuser, $wgDBpassword, $wgDBtype, $wgSQLiteDataDir;
94
95 $servers = [
96 [ // master
97 'host' => $wgDBserver,
98 'dbname' => $wgDBname,
99 'user' => $wgDBuser,
100 'password' => $wgDBpassword,
101 'type' => $wgDBtype,
102 'dbDirectory' => $wgSQLiteDataDir,
103 'load' => 0,
104 'flags' => DBO_TRX // REPEATABLE-READ for consistency
105 ],
106 [ // emulated slave
107 'host' => $wgDBserver,
108 'dbname' => $wgDBname,
109 'user' => $wgDBuser,
110 'password' => $wgDBpassword,
111 'type' => $wgDBtype,
112 'dbDirectory' => $wgSQLiteDataDir,
113 'load' => 100,
114 'flags' => DBO_TRX // REPEATABLE-READ for consistency
115 ]
116 ];
117
118 $factory = new LBFactorySimple( [
119 'servers' => $servers,
120 'loadMonitorClass' => 'LoadMonitorNull'
121 ] );
122 $lb = $factory->getMainLB();
123
124 $dbw = $lb->getConnection( DB_MASTER );
125 $this->assertTrue( $dbw->getLBInfo( 'master' ), 'master shows as master' );
126 $this->assertEquals(
127 ( $wgDBserver != '' ) ? $wgDBserver : 'localhost',
128 $dbw->getLBInfo( 'clusterMasterHost' ),
129 'cluster master set' );
130
131 $dbr = $lb->getConnection( DB_SLAVE );
132 $this->assertTrue( $dbr->getLBInfo( 'replica' ), 'slave shows as slave' );
133 $this->assertEquals(
134 ( $wgDBserver != '' ) ? $wgDBserver : 'localhost',
135 $dbr->getLBInfo( 'clusterMasterHost' ),
136 'cluster master set' );
137
138 $factory->shutdown();
139 $lb->closeAll();
140 }
141
142 public function testLBFactoryMulti() {
143 global $wgDBserver, $wgDBname, $wgDBuser, $wgDBpassword, $wgDBtype, $wgSQLiteDataDir;
144
145 $factory = new LBFactoryMulti( [
146 'sectionsByDB' => [],
147 'sectionLoads' => [
148 'DEFAULT' => [
149 'test-db1' => 0,
150 'test-db2' => 100,
151 ],
152 ],
153 'serverTemplate' => [
154 'dbname' => $wgDBname,
155 'user' => $wgDBuser,
156 'password' => $wgDBpassword,
157 'type' => $wgDBtype,
158 'dbDirectory' => $wgSQLiteDataDir,
159 'flags' => DBO_DEFAULT
160 ],
161 'hostsByName' => [
162 'test-db1' => $wgDBserver,
163 'test-db2' => $wgDBserver
164 ],
165 'loadMonitorClass' => 'LoadMonitorNull'
166 ] );
167 $lb = $factory->getMainLB();
168
169 $dbw = $lb->getConnection( DB_MASTER );
170 $this->assertTrue( $dbw->getLBInfo( 'master' ), 'master shows as master' );
171
172 $dbr = $lb->getConnection( DB_SLAVE );
173 $this->assertTrue( $dbr->getLBInfo( 'replica' ), 'slave shows as slave' );
174
175 $factory->shutdown();
176 $lb->closeAll();
177 }
178
179 public function testChronologyProtector() {
180 // (a) First HTTP request
181 $mPos = new MySQLMasterPos( 'db1034-bin.000976', '843431247' );
182
183 $now = microtime( true );
184 $mockDB = $this->getMockBuilder( 'DatabaseMysqli' )
185 ->disableOriginalConstructor()
186 ->getMock();
187 $mockDB->method( 'writesOrCallbacksPending' )->willReturn( true );
188 $mockDB->method( 'lastDoneWrites' )->willReturn( $now );
189 $mockDB->method( 'getMasterPos' )->willReturn( $mPos );
190
191 $lb = $this->getMockBuilder( 'LoadBalancer' )
192 ->disableOriginalConstructor()
193 ->getMock();
194 $lb->method( 'getConnection' )->willReturn( $mockDB );
195 $lb->method( 'getServerCount' )->willReturn( 2 );
196 $lb->method( 'parentInfo' )->willReturn( [ 'id' => "main-DEFAULT" ] );
197 $lb->method( 'getAnyOpenConnection' )->willReturn( $mockDB );
198 $lb->method( 'hasOrMadeRecentMasterChanges' )->will( $this->returnCallback(
199 function () use ( $mockDB ) {
200 $p = 0;
201 $p |= call_user_func( [ $mockDB, 'writesOrCallbacksPending' ] );
202 $p |= call_user_func( [ $mockDB, 'lastDoneWrites' ] );
203
204 return (bool)$p;
205 }
206 ) );
207 $lb->method( 'getMasterPos' )->willReturn( $mPos );
208
209 $bag = new HashBagOStuff();
210 $cp = new ChronologyProtector(
211 $bag,
212 [
213 'ip' => '127.0.0.1',
214 'agent' => "Totally-Not-FireFox"
215 ]
216 );
217
218 $mockDB->expects( $this->exactly( 2 ) )->method( 'writesOrCallbacksPending' );
219 $mockDB->expects( $this->exactly( 2 ) )->method( 'lastDoneWrites' );
220
221 // Nothing to wait for
222 $cp->initLB( $lb );
223 // Record in stash
224 $cp->shutdownLB( $lb );
225 $cp->shutdown();
226
227 // (b) Second HTTP request
228 $cp = new ChronologyProtector(
229 $bag,
230 [
231 'ip' => '127.0.0.1',
232 'agent' => "Totally-Not-FireFox"
233 ]
234 );
235
236 $lb->expects( $this->once() )
237 ->method( 'waitFor' )->with( $this->equalTo( $mPos ) );
238
239 // Wait
240 $cp->initLB( $lb );
241 // Record in stash
242 $cp->shutdownLB( $lb );
243 $cp->shutdown();
244 }
245
246 private function newLBFactoryMulti( array $baseOverride = [], array $serverOverride = [] ) {
247 global $wgDBserver, $wgDBuser, $wgDBpassword, $wgDBname, $wgDBtype, $wgSQLiteDataDir;
248
249 return new LBFactoryMulti( $baseOverride + [
250 'sectionsByDB' => [],
251 'sectionLoads' => [
252 'DEFAULT' => [
253 'test-db1' => 1,
254 ],
255 ],
256 'serverTemplate' => $serverOverride + [
257 'dbname' => $wgDBname,
258 'user' => $wgDBuser,
259 'password' => $wgDBpassword,
260 'type' => $wgDBtype,
261 'dbDirectory' => $wgSQLiteDataDir,
262 'flags' => DBO_DEFAULT
263 ],
264 'hostsByName' => [
265 'test-db1' => $wgDBserver,
266 ],
267 'loadMonitorClass' => 'LoadMonitorNull',
268 'localDomain' => wfWikiID()
269 ] );
270 }
271
272 public function testNiceDomains() {
273 global $wgDBname, $wgDBtype;
274
275 if ( $wgDBtype === 'sqlite' ) {
276 $tmpDir = $this->getNewTempDirectory();
277 $dbPath = "$tmpDir/unit_test_db.sqlite";
278 file_put_contents( $dbPath, '' );
279 $tempFsFile = new TempFSFile( $dbPath );
280 $tempFsFile->autocollect();
281 } else {
282 $dbPath = null;
283 }
284
285 $factory = $this->newLBFactoryMulti(
286 [],
287 [ 'dbFilePath' => $dbPath ]
288 );
289 $lb = $factory->getMainLB();
290
291 if ( $wgDBtype !== 'sqlite' ) {
292 $db = $lb->getConnectionRef( DB_MASTER );
293 $this->assertEquals(
294 $wgDBname,
295 $db->getDomainID()
296 );
297 unset( $db );
298 }
299
300 /** @var Database $db */
301 $db = $lb->getConnection( DB_MASTER, [], '' );
302 $lb->reuseConnection( $db ); // don't care
303
304 $this->assertEquals(
305 '',
306 $db->getDomainID()
307 );
308
309 $this->assertEquals(
310 $this->quoteTable( $db, 'page' ),
311 $db->tableName( 'page' ),
312 "Correct full table name"
313 );
314
315 $this->assertEquals(
316 $this->quoteTable( $db, $wgDBname ) . '.' . $this->quoteTable( $db, 'page' ),
317 $db->tableName( "$wgDBname.page" ),
318 "Correct full table name"
319 );
320
321 $this->assertEquals(
322 $this->quoteTable( $db, 'nice_db' ) . '.' . $this->quoteTable( $db, 'page' ),
323 $db->tableName( 'nice_db.page' ),
324 "Correct full table name"
325 );
326
327 $factory->setDomainPrefix( 'my_' );
328 $this->assertEquals(
329 '',
330 $db->getDomainID()
331 );
332 $this->assertEquals(
333 $this->quoteTable( $db, 'my_page' ),
334 $db->tableName( 'page' ),
335 "Correct full table name"
336 );
337 $this->assertEquals(
338 $this->quoteTable( $db, 'other_nice_db' ) . '.' . $this->quoteTable( $db, 'page' ),
339 $db->tableName( 'other_nice_db.page' ),
340 "Correct full table name"
341 );
342
343 $factory->closeAll();
344 $factory->destroy();
345 }
346
347 public function testTrickyDomain() {
348 global $wgDBtype;
349
350 if ( $wgDBtype === 'sqlite' ) {
351 $tmpDir = $this->getNewTempDirectory();
352 $dbPath = "$tmpDir/unit_test_db.sqlite";
353 file_put_contents( $dbPath, '' );
354 $tempFsFile = new TempFSFile( $dbPath );
355 $tempFsFile->autocollect();
356 } else {
357 $dbPath = null;
358 }
359
360 $dbname = 'unittest-domain';
361 $factory = $this->newLBFactoryMulti(
362 [ 'localDomain' => $dbname ],
363 [ 'dbname' => $dbname, 'dbFilePath' => $dbPath ]
364 );
365 $lb = $factory->getMainLB();
366 /** @var Database $db */
367 $db = $lb->getConnection( DB_MASTER, [], '' );
368 $lb->reuseConnection( $db ); // don't care
369
370 $this->assertEquals(
371 '',
372 $db->getDomainID()
373 );
374
375 $this->assertEquals(
376 $this->quoteTable( $db, 'page' ),
377 $db->tableName( 'page' ),
378 "Correct full table name"
379 );
380
381 $this->assertEquals(
382 $this->quoteTable( $db, $dbname ) . '.' . $this->quoteTable( $db, 'page' ),
383 $db->tableName( "$dbname.page" ),
384 "Correct full table name"
385 );
386
387 $this->assertEquals(
388 $this->quoteTable( $db, 'nice_db' ) . '.' . $this->quoteTable( $db, 'page' ),
389 $db->tableName( 'nice_db.page' ),
390 "Correct full table name"
391 );
392
393 $factory->setDomainPrefix( 'my_' );
394
395 $this->assertEquals(
396 $this->quoteTable( $db, 'my_page' ),
397 $db->tableName( 'page' ),
398 "Correct full table name"
399 );
400 $this->assertEquals(
401 $this->quoteTable( $db, 'other_nice_db' ) . '.' . $this->quoteTable( $db, 'page' ),
402 $db->tableName( 'other_nice_db.page' ),
403 "Correct full table name"
404 );
405
406 \MediaWiki\suppressWarnings();
407 $this->assertFalse( $db->selectDB( 'garbage-db' ) );
408 \MediaWiki\restoreWarnings();
409
410 $this->assertEquals(
411 $this->quoteTable( $db, 'garbage-db' ) . '.' . $this->quoteTable( $db, 'page' ),
412 $db->tableName( 'garbage-db.page' ),
413 "Correct full table name"
414 );
415
416 $factory->closeAll();
417 $factory->destroy();
418 }
419
420 private function quoteTable( Database $db, $table ) {
421 if ( $db->getType() === 'sqlite' ) {
422 return $table;
423 } else {
424 return $db->addIdentifierQuotes( $table );
425 }
426 }
427 }